"""
    @project_name: Fishing_Calendar
    @author：henry_oulen@163.com
    @file： send_wx.py.py
    @date：2023/5/10 15:22
"""
import json
import time
import traceback
from datetime import datetime

import requests
from tornado import ioloop

from json_tool.utils import get_dat, download_page, get_content
from wx_tools.config_wx import WEB_HOOK


def error_handler(func):
    def wrapper(*args, **kwargs):
        try:
            f_time = int(time.time())
            print("[start]-[%s]-[%s]-[timestamp:%s]" % (datetime.now().strftime('%Y-%m-%d %H:%M:%S'), f_time, func))
            res = func(*args, **kwargs)
            e_time = int(time.time())
            print("[final]-[%s]-[%s]-[timestamp:%s]-[consuming:%s]" % (
                datetime.now().strftime('%Y-%m-%d %H:%M:%S'), e_time, e_time - f_time, func))
            return res
        except:
            msg = traceback.format_exc()
            print("[error]-[%s]-[%s]-[error_msg:%s]" % (datetime.now().strftime('%Y-%m-%d %H:%M:%S'), func, msg))

    return wrapper  # 返回函数名



# 发送文本消息
def send_text(webhook, content, mentioned_list=None, mentioned_mobile_list=None):
    header = {
        "Content-Type": "application/json",
        "Charset": "UTF-8"
    }
    data = {
        "msgtype": "text",
        "text": {
            "content": content
            , "mentioned_list": mentioned_list
            , "mentioned_mobile_list": mentioned_mobile_list
        }
    }

    data = json.dumps(data)
    info = requests.post(url=webhook, data=data, headers=header)

def get_content_last():
    last_send = get_dat()
    send = ""
    for i in last_send:
        send += i + "\r\n"
    return send

def get_hot_news():
    #     #      综合   科技   娱乐    社区        购物      财经
    #     #page=['news','tech','ent','community','shopping','finance']
    page = ['news']
    last_send = []
    _v = "hot"
    try:
        for tag in page:
            url = 'https://tophub.today/c/{}'.format(tag)
            html = download_page(url)
            return_list = get_content(html, tag, _v)
            last_send = last_send + return_list
    except Exception as e:
        pass
    last_send.append("---------------------------")
    last_content = "认认真真上班，这根本就不叫赚钱，那是用劳动换取报酬。"
    _last_content = "人活半辈子才明白，真正高级的养生，是回家好好吃饭"
    _last_content0 = "别沮丧，生活就像心电图，一帆风顺就证明你挂了！"
    _last_content1 = "最后，祝愿天下所有摸鱼人，都能愉快的渡过每一天!"
    _last_content2 = "关注我，这样每天我都能见到你了"
    _last_content3 = "我是 【捣蛋小能手】 捣蛋的捣 & 捣蛋的蛋 ! "
    _last_content4 = "========Email:henry_oulen@163.com======="

    last_send.append(last_content)
    last_send.append(_last_content)
    last_send.append(_last_content0)
    last_send.append(_last_content1)
    last_send.append(_last_content2)
    last_send.append(_last_content3)
    last_send.append(_last_content4)
    send = ""
    for i in last_send:
        send += i + "\r\n"
    return send
    # tkinter.messagebox.showinfo(title='my message', message=send)


def get_today():
    last_send = []
    #     #      综合   科技   娱乐    社区        购物      财经
    #     #page=['news','tech','ent','community','shopping','finance']
    page = ['news']
    _v = "today"
    try:
        for tag in page:
            url = 'https://tophub.today/c/{}'.format(tag)
            html = download_page(url)
            return_list = get_content(html, tag, _v)
            last_send = last_send + return_list
    except Exception as e:
        pass
    last_send.append("---------------------------")
    last_content = "认认真真上班，这根本就不叫赚钱，那是用劳动换取报酬。"
    _last_content = "人活半辈子才明白，真正高级的养生，是回家好好吃饭"
    _last_content0 = "别沮丧，生活就像心电图，一帆风顺就证明你挂了！"
    _last_content1 = "最后，祝愿天下所有摸鱼人，都能愉快的渡过每一天!"
    _last_content2 = "关注我，这样每天我都能见到你了"
    _last_content3 = "我是 【捣蛋小能手】 捣蛋的捣 & 捣蛋的蛋 ! "
    _last_content4 = "========Email:henry_oulen@163.com======="

    last_send.append(last_content)
    last_send.append(_last_content)
    last_send.append(_last_content0)
    last_send.append(_last_content1)
    last_send.append(_last_content2)
    last_send.append(_last_content3)
    last_send.append(_last_content4)
    send = ""
    for i in last_send:
        send += i + "\r\n"
    return send

@error_handler
def start():
    hours = (datetime.now()).strftime("%Y-%m-%d ,%H,:%M")
    hours = hours.split(",")[1]
    if hours == "09":
        webhook = WEB_HOOK
        content = get_content_last()
        content1 = get_hot_news()
        content2 = get_today()
        send_text(webhook=webhook, content=content)
        send_text(webhook=webhook, content=content1)
        send_text(webhook=webhook, content=content2)

if __name__ == '__main__':
    print("[start]-[init orm_cron]-[%s]" % (datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
    start()
    print("[start]-[init exec]-[%s]" % (datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
    ioloop.PeriodicCallback(start, 1000 * 60 * 60).start()  # 注意单位  1000是毫秒  后面是秒 在后面是分钟
    ioloop.IOLoop.current().start()
    print("[end] [init exec] [%s]" % (datetime.now().strftime('%Y-%m-%d %H:%M:%S')))